import { createWebHistory, createRouter, type RouteRecord, type RouteLocationNormalized, type NavigationGuardNext } from 'vue-router';
/* Layout */
import {useUserStore} from '../store/modules/user';
// 公共路由
export const constantRoutes: RouteRecord[] = [
    {
      path: '/',
      name: 'Home',
      component: () => import('@/views/home/index.vue'),
      meta: { 
        title: '首页',
        requiresAuth: false
      }
    },
    {
      path: '/dashboard',
      component: () => import('@/views/dashboard/index.vue'),
      hidden: true,
      redirect: '/dashboard/tenderWriting',
      children: [
        {
          path: 'tenderWriting',
          component: () => import('@/views/tenderWriting/index.vue'),
          meta: {
            affixTab: true,
            title: '标书书写',
            icon: 'EditPen',
            requiresAuth: true
          }
        },
        {
          path: 'addTender',
          component: () => import('@/views/tenderWriting/components/add-tender.vue'),
          hidden: true,
          meta: {
            title: '新建标书',
            activeMenu: '/dashboard/tenderWriting'
          }
        },
        {
          path: 'catalogTree',
          component: () => import('@/views/tenderWriting/components/catalogTree.vue'),
          hidden: true,
          meta: {
            title: '标书正文',
            activeMenu: '/dashboard/catalogTree'
          }
        },
        /* {
          path: 'chapterGen',
          component: () => import('@/views/tenderWriting/components/chapter-gen.vue'),
          hidden: true,
          meta: {
            title: '编写专项章节',
            activeMenu: '/dashboard/chapterGen'
          }
        }, */
        {
          path: 'nationalStandard',
          component: () => import('@/views/nationalStandard/index.vue'),
          hidden: true,
          meta: {
            affixTab: true,
            title: '全国标讯',
            icon: 'DocumentRemove'
          }
        },
        {
          path: 'nationalStandardDetail',
          component: () => import('@/views/nationalStandard/components/nationalStandardDetail.vue'),
          hidden: true,
          meta: {
            title: '标讯详情',
            activeMenu: '/dashboard/nationalStandard'
          }
        },
        {
          path: 'enterpriseKnowledge',
          component: () => import('@/views/enterpriseKnowledge/index.vue'),
          hidden: true,
          meta: {
            affixTab: true,
            title: '企业文档',
            icon: 'Tickets'
          }
        },
        {
          path: 'enterpriseKnowledgeDetail',
          component: () => import('@/views/enterpriseKnowledge/components/enterpriseKnowledgeDetail.vue'),
          hidden: true,
          meta: {
            title: '知识库详情',
            activeMenu: '/dashboard/enterpriseKnowledge'
          }
        },
        {
          path: 'subscriptionEnterprise',
          component: () => import('@/views/subscriptionEnterprise/index.vue'),
          hidden: true,
          meta: {
            affixTab: true,
            title: '订阅企业',
            icon: 'User'
          }
        },
        {
          path: 'myBids',
          component: () => import('@/views/myBids/index.vue'),
          meta: {
            affixTab: true,
            title: '我的标书',
            icon: 'Notebook'
          }
        },
        {
          path: 'answer',
          component: () => import('@/views/answer/index.vue'),
          hidden: true,
          meta: {
            affixTab: true,
            title: 'AI问答',
            icon: 'Search'
          }
        },
        // {
        //   path: 'review',
        //   component: () => import('@/views/Review.vue'),
        //   meta: {
        //     affixTab: true,
        //     title: '人工审核',
        //     icon: 'Check',
        //     requiresAuth: true
        //   }
        // },
      ]
    },
    {
      path: '/login',
      component: () => import('@/views/login/index.vue'),
      hidden: true
    },
    {
      path: '/:pathMatch(.*)*',
      component: () => import('@/views/error/404.vue'),
      hidden: true
    },
    {
      path: '/401',
      component: () => import('@/views/error/401.vue'),
      hidden: true
    },
  ];
  /**
 * 创建路由
 */
const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: constantRoutes,
    // 刷新时，滚动条位置还原
    scrollBehavior(to, from, savedPosition) {
      if (savedPosition) {
        return savedPosition;
      }
      return { top: 0 };
    }
  });
  
  // 白名单，不需要登录就可以访问的路由
  const whiteList = ['/login', '/register', '/forgot-password'];
  
  // 全局前置守卫
  router.beforeEach(async (to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext) => {
    const userStore = useUserStore();
    const isAuthenticated = userStore.isLoggedIn();
    console.log(isAuthenticated)
  
    // 如果用户已登录且访问首页，重定向到标书编写页面
    if (isAuthenticated && to.path === '/') {
      next('/dashboard/tenderWriting');
      return;
    }
  
    // 如果需要登录但未登录，则重定向到登录页面
    if (to.meta.requiresAuth && !isAuthenticated) {
      next(`/login?redirect=${to.fullPath}`);
      return;
    }
  
    next();
  });
  
  export default router;